package com.android.equipment.ui.people

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.android.equipment.R
import com.android.equipment.adapter.PeopleManagerAdapter
import com.android.equipment.base.BaseActivity
import com.android.equipment.dao.People
import com.android.equipment.dao.operate.DaoPeople
import com.android.equipment.databinding.ActivityPeopleManagerBinding
import com.gyf.immersionbar.ImmersionBar
import com.hjq.bar.OnTitleBarListener
import com.lxj.xpopup.XPopup

class PeopleManagerActivity : BaseActivity<ActivityPeopleManagerBinding>() {
    private var peopleManagerAdapter: PeopleManagerAdapter? = null
    private var peopleList: List<People>? = null

    override fun setLayoutContent(): ActivityPeopleManagerBinding {
        return ActivityPeopleManagerBinding.inflate(layoutInflater)
    }

    @Throws(Exception::class)
    override fun initView(savedInstanceState: Bundle?) {
        initRecycler()
    }

    override fun initData() {
    }

    override fun onResume() {
        super.onResume()
        refreshData()
    }

    private fun initRecycler() {
        peopleManagerAdapter = PeopleManagerAdapter(R.layout.item_people_manager)
        binding.rvPeopleManager.adapter = peopleManagerAdapter

        peopleManagerAdapter!!.setOnItemLongClickListener { adapter, view, position ->
            val people = peopleManagerAdapter!!.getItem(position)
            XPopup.Builder(this@PeopleManagerActivity)
                .atView(view) // 依附于所点击的View，内部会自动判断在上方或者下方显示
                .asAttachList(
                    arrayOf<String>("修改", "删除"),
                    intArrayOf()
                ) { position, text ->
                    if (position == 0) {
                        val intent =
                            Intent(this@PeopleManagerActivity, EditPeopleActivity::class.java)
                        intent.putExtra("id", people.id)
                        mSwipeBackHelper.forward(intent)
                    } else {
                        DaoPeople.getInstance().deletePeople(people)
                        refreshData()
                    }
                }
                .show()
            false
        }
    }

    private fun refreshData() {
        peopleList = DaoPeople.getInstance().notDelPeople
        peopleList?.let {
            peopleManagerAdapter!!.setList(it)
        }


    }

    override fun initEvent() {
        super.initEvent()
        binding.mTitleBar.setOnTitleBarListener(object : OnTitleBarListener {
            override fun onLeftClick(v: View) {
                mSwipeBackHelper.backward()
            }

            override fun onTitleClick(v: View) {
            }

            override fun onRightClick(v: View) {
                mSwipeBackHelper.forward(EditPeopleActivity::class.java)
            }
        })
    }

    override fun setStatusBar() {
        ImmersionBar.with(this)
            .fullScreen(false)
            .statusBarColor(R.color.white)
            .statusBarDarkFont(true)
            .titleBar(binding.mTitleBar)
            .init()
    }
}
